package com.example.second;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MemberListActivity extends Activity {
    private static final int CONTEXT_MENU_DELETE = 0x0001;

    private Context context;

    private ArrayList<MemberItem> memberList = new ArrayList<MemberItem>();
    private ListAdapter adapter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memberlist);

        context = this;
        updateList();
        adapter = new ListAdapter(context, memberList);

        listView = (ListView)findViewById(R.id.memberlist_list);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_memberlist, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.menu_add) {
            Intent intent = new Intent(context, SignUpActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        menu.add(ContextMenu.NONE, CONTEXT_MENU_DELETE, CONTEXT_MENU_DELETE, "Delete");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

        switch(item.getItemId()) {
            case CONTEXT_MENU_DELETE : {
                MemberItem member = memberList.get((int) info.id);
//                FileData fileData = mFileList.get((int) info.id);
//
//                Intent intent = new Intent("android.intent.action.TRIM");
//                intent.putExtra("uri",Uri.fromFile(new File(fileData.getPath())));
//                startActivity(intent);
            }
            return true;

            default:
            return super.onContextItemSelected(item);
        }
    }

    @Override
    protected void onRestart() {
        log.i("MemberListActivity - onRestart");
        super.onRestart();
    }

    @Override
    protected void onStart() {
        log.i("MemberListActivity - onStart");
        super.onStart();
    }


    @Override
    protected void onResume() {
        log.i("MemberListActivity - onResume");
        super.onResume();
    }


    @Override
    protected void onPause() {
        log.i("MemberListActivity - onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        log.i("MemberListActivity - onStop");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        log.i("MemberListActivity - onDestroy");
        unregisterForContextMenu(listView);
        super.onDestroy();
    }

    private void updateList() {

        memberList.clear();

        memberList.add(new MemberItem("KyungHwan Kim", "010-4494-5678", "KKH", "1234"));
        memberList.add(new MemberItem("Ktttt tt", "010-4494-1234", "KGSFS", "1234"));
    }

    private class MemberItem {
        private static final int icon = R.drawable.member_ic;

        private String name;
        private String mobile;
        private String id;
        private String pass;

        public MemberItem(String name, String mobile, String id, String pass) {
            this.name = name;
            this.mobile = mobile;
            this.id = id;
            this.pass = pass;
        }

        public int getIconId() {
            return icon;
        }

        public String getName() {
            return name;
        }

        public String getMobile() {
            return mobile;
        }

        public String getId() {
            return id;
        }

        public String getPassword() {
            return pass;
        }
    }

    public class ListViewWrapper {
        private View base;
        private ImageView icon;
        private TextView name;
        private TextView mobile;

        public ListViewWrapper(View base) {
            this.base = base;

            this.icon = null;
            this.name = null;
            this.mobile = null;
        }

        public ImageView getIcon() {
            if(icon == null) {
                icon = (ImageView)base.findViewById(R.id.memberlist_row_icon);
            }
            return icon;
        }

        public TextView getName() {
            if(name == null) {
                name = (TextView)base.findViewById(R.id.memberlist_row_name);
            }
            return name;
        }

        public TextView getMobile() {
            if(mobile == null) {
                mobile = (TextView)base.findViewById(R.id.memberlist_row_mobile);
            }
            return mobile;
        }
    }

    private class ListAdapter extends ArrayAdapter<MemberItem> {
        private LayoutInflater inflater;

        public ListAdapter(Context context, ArrayList<MemberItem> objects) {
            super(context, 0, objects);
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            ListViewWrapper viewWrapper;

            if(view == null) {
                view = inflater.inflate(R.layout.activity_memberlist_row, null);
                viewWrapper = new ListViewWrapper(view);
                view.setTag(viewWrapper);
            } else {
                viewWrapper = (ListViewWrapper)view.getTag();
            }

            final MemberItem data = this.getItem(position);
            if(data != null) {
                viewWrapper.getIcon().setImageResource(data.getIconId());
                viewWrapper.getName().setText(data.getName());
                viewWrapper.getMobile().setText(data.getMobile());
            }

            return view;
        }
    }
}
